#include "powerup.h"

powerup::powerup(){
	p_sprite = NULL;
	p_spriteAnim = NULL;

	p_type=0;
	p_size=32;

	markedForDeletion=false;

	p_position.x=0.0;
	p_position.y=0.0;
	p_localPosition.x=0.0;
	p_localPosition.y=0.0;
	p_offset.x=0.0;
	p_offset.y=0.0;
	setBounds();
}

powerup::powerup(const powerup &P){
	p_sprite = P.p_sprite;
	p_spriteAnim = P.p_spriteAnim;
	p_type = P.p_type;
	p_sizeOffset = P.p_sizeOffset;
	p_size = P.p_size;
	p_position = P.p_position;
	p_localPosition = P.p_localPosition;
	p_offset = P.p_offset;
	p_bounds = P.p_bounds;
}

powerup::~powerup(){
	//p_sprite->isHidden=true;
	//printf("powerup deleted\n");
	lnSpriteDestroy(p_sprite);
}

void powerup::assignSprite(){
		p_sprite = lnSpriteCreate(SCREEN_MAIN, SPRSIZE_32x32,COLMODE_16,powerupsTiles,powerupsPal);
		p_spriteAnim = lnSpriteAnimBind(p_sprite,powerupsTiles,1,7);
		p_sprite->isHidden=true;
}

void powerup::update(){
	setBounds();
	handleAnimation();
	if(p_type != 0){
		display();
	}
}

void powerup::display(){
	p_localPosition.x=p_position.x-p_offset.x;
	p_localPosition.y=p_position.y-p_offset.y;
	if((p_localPosition.x < -32.0) || (p_localPosition.x > (Fixed)SCREEN_WIDTH+32.0)){
		p_sprite->isHidden=true;
	}else if((p_localPosition.y < -32.0) || (p_localPosition.y > (Fixed)SCREEN_HEIGHT+32.0)){
		p_sprite->isHidden=true;
	}else{
		p_sprite->isHidden=false;
	}
	//printf("%f %f\n",(float)p_localPosition.x,(float)p_localPosition.y);
	//printf("%f %f\n",(float)p_position.x,(float)p_position.y);
	p_sprite->posX=(int)p_localPosition.x;
	p_sprite->posY=(int)p_localPosition.y;
}

void powerup::handleAnimation(){
	if(p_type == 0){
		p_spriteAnim->animStart = 0;
		p_spriteAnim->animEnd = 5;
		lnSpriteAnimLoop(p_sprite,ANIMTYPE_LOOP,0,1);
	}else if(p_type == 1){
		p_spriteAnim->animStart = 0;
		p_spriteAnim->animEnd = 1;
		lnSpriteAnimLoop(p_sprite,ANIMTYPE_LOOP,0,1);
	}else if(p_type == 2){
		p_spriteAnim->animStart = 2;
		p_spriteAnim->animEnd = 3;
		lnSpriteAnimLoop(p_sprite,ANIMTYPE_LOOP,0,1);
	}else if(p_type == 3){
		p_spriteAnim->animStart = 4;
		p_spriteAnim->animEnd = 5;
		lnSpriteAnimLoop(p_sprite,ANIMTYPE_LOOP,0,1);
	}else if(p_type == 4){
		p_spriteAnim->animStart = 6;
		p_spriteAnim->animEnd = 7;
		lnSpriteAnimLoop(p_sprite,ANIMTYPE_LOOP,0,1);
	}else if(p_type == 5){
		p_spriteAnim->animStart = 6;
		p_spriteAnim->animEnd = 7;
		lnSpriteAnimLoop(p_sprite,ANIMTYPE_LOOP,0,1);
	}else if(p_type > 5){
		p_spriteAnim->animStart=p_type+2;
		p_spriteAnim->animEnd=p_type+2;
		lnSpriteAnimLoop(p_sprite,ANIMTYPE_LOOP,0,1);
	}
}

void powerup::setOffset(tFloat posX, tFloat posY){
	p_offset.x = posX;
	p_offset.y = posY;
}

void powerup::setPos(tFloat posX, tFloat posY){
	p_position.x = posX;
	p_position.y = posY;
	setBounds();
}

void powerup::setType(int type){
	p_type=type;
}

void powerup::setBounds(){
	p_bounds.top = p_position.y;
	p_bounds.bottom = p_position.y + (float)p_size;
	p_bounds.right = p_position.x+ (float)p_size;
	p_bounds.left = p_position.x;
}

void powerup::setHidden(bool hidden){
	p_sprite->isHidden=hidden;
}

void powerup::activatePowerup(){
	if(p_type ==1){
		//Speedup powerup
		Player* player= Player::getInstance();
		player->speedUp();
		scorekeeper* Score = scorekeeper::getInstance();
		Score->increaseScore(500);
	}else if(p_type == 2){
		Player* player= Player::getInstance();
		player->healthPack();
		scorekeeper* Score = scorekeeper::getInstance();
		Score->increaseScore(500);
	}else if(p_type == 3){
		Player* player= Player::getInstance();
		player->addAmmo();
		scorekeeper* Score = scorekeeper::getInstance();
		Score->increaseScore(500);
	}else if(p_type==4){
		//End of level
		p_sprite->isHidden=true;
		scorekeeper *Score = scorekeeper::getInstance();
		Score->levelReport();
		game* Game = game::getInstance();
		Game->advanceLevel();
		Game->cutScene(Game->getCurrentLevel());
		Game->startLevel(Game->getCurrentLevel());
		//Game->resetLevel();
	}else if(p_type==5){
		//End of game
		Player* player= Player::getInstance();
		//Teleport player to boss fight.
		player->setPos(373.0*16.0, 33.0*16.0);
	}else if((p_type > 5)&&(p_type < 22)){
		scorekeeper* Score = scorekeeper::getInstance();
		Score->increaseScore(2500);
	}else if(p_type == 22){
		scorekeeper* Score = scorekeeper::getInstance();
		Score->increaseScore(15000);
	}
}

BoundingBox powerup::getBounds(){
	return p_bounds;
}

t2DVector powerup::getPos(){
	return p_position;
}